<!-- 绿色通道 - 患者列表 - 医疗记录 - 急诊接诊 界面 -->
<template>
  <div class="info">
    <el-row type="flex" justify="end" style="padding-right: 40px">
      <el-button type="primary" @click="update">保存</el-button>
    </el-row>
    <el-form ref="patientForm" :model="patientForm" :rules="rules" label-width="128px" class="pageForm">
      <el-row class="form-row">
        <el-col :span="24">
          <el-col :span="8">
            <el-form-item label="首次医疗接触时间" label-width="200px" required>
              <el-date-picker
                v-model="patientForm.dispatchEmergens.firstMedicalContactDateTime"
                type="datetime"
                size="mini"
                value-format="yyyy-MM-dd HH:mm:ss"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="到达医院大门时间" label-width="200px" required>
              <el-date-picker
                v-model="patientForm.dispatchEmergens.arrivalTime"
                type="datetime"
                size="mini"
                value-format="yyyy-MM-dd HH:mm:ss"
              />
            </el-form-item>
          </el-col>
        </el-col>
        <el-col :span="24">
          <el-col :span="8">
            <el-form-item label="患者发病时间" label-width="200px" required>
              <el-date-picker
                v-model="patientForm.incidentDateTime"
                type="datetime"
                size="mini"
                value-format="yyyy-MM-dd HH:mm:ss"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="最后正常时间" label-width="200px">
              <el-date-picker
                v-model="patientForm.lastNormalTime"
                type="datetime"
                size="mini"
                value-format="yyyy-MM-dd HH:mm:ss"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label=" " label-width="200px">
              <el-checkbox v-model="patientForm.isKnownAttackTime" true-label="3" false-label="0">醒后卒中</el-checkbox>
              <!-- <el-radio-group v-model="patientForm.isKnownAttackTime">
              		<el-radio label="" >否</el-radio>
              		<el-radio label="3">是</el-radio>
              </el-radio-group> -->
            </el-form-item>
          </el-col>
        </el-col>
        <el-col :span="24">
          <el-form-item label="入院途径" required>
            <el-radio-group v-model="patientForm.coming_institution_code">
              <el-radio label="1">急诊</el-radio>
              <el-radio label="2">门诊</el-radio>
              <el-radio label="3">其他医疗机构转入</el-radio>
              <el-radio label="4">其他</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="来院方式" required>
            <el-radio
              v-model="patientForm.comingWayCode"
              label="6"
              @change="patientForm.dispatchEmergens.cwZyTransType = ''"
            >
              本院急救车
            </el-radio>
            <el-radio
              v-model="patientForm.comingWayCode"
              label="7"
              @change="patientForm.dispatchEmergens.cwZyTransType = ''"
            >
              当地120
            </el-radio>
            <el-radio
              v-model="patientForm.comingWayCode"
              label="9"
              @change="patientForm.dispatchEmergens.cwZyTransType = ''"
            >
              自行来院
            </el-radio>
            <el-radio
              v-model="patientForm.comingWayCode"
              label="10"
              @change="patientForm.dispatchEmergens.cwZyTransType = ''"
            >
              在院卒中
            </el-radio>
            <el-radio
              v-model="patientForm.dispatchEmergens.cwZyTransType"
              label="1"
              @click.native="patientForm.comingWayCode = '8'"
            >
              外院转院-网络协作医院
            </el-radio>
            <el-radio
              v-model="patientForm.dispatchEmergens.cwZyTransType"
              label="2"
              @click.native="patientForm.comingWayCode = '8'"
            >
              外院转院-非网络协作医院
            </el-radio>
          </el-form-item>
          <div v-if="patientForm.comingWayCode === '6' || patientForm.comingWayCode === '7'">
            <el-col :span="8">
              <el-form-item label="呼救时间" label-width="200px">
                <el-date-picker
                  v-model="patientForm.dispatchEmergens.helpDate"
                  type="datetime"
                  size="mini"
                  value-format="yyyy-MM-dd HH:mm:ss"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="120派车时间" label-width="200px">
                <el-date-picker
                  v-model="patientForm.dispatchEmergens.dispatch120Time"
                  type="datetime"
                  size="mini"
                  value-format="yyyy-MM-dd HH:mm:ss"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="120出车时间" label-width="200px">
                <el-date-picker
                  v-model="patientForm.dispatchEmergens.departure120Time"
                  type="datetime"
                  size="mini"
                  value-format="yyyy-MM-dd HH:mm:ss"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="120到达现场时间" label-width="200px">
                <el-date-picker
                  v-model="patientForm.dispatchEmergens.arrivalScene120Time"
                  type="datetime"
                  size="mini"
                  value-format="yyyy-MM-dd HH:mm:ss"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="120离开现场时间" label-width="200px">
                <el-date-picker
                  v-model="patientForm.dispatchEmergens.leaveScene120Time"
                  type="datetime"
                  size="mini"
                  value-format="yyyy-MM-dd HH:mm:ss"
                />
              </el-form-item>
            </el-col>
            <!-- <el-col :span="8">
              <el-form-item label="患者送达医院时间" label-width="200px">
                <el-date-picker v-model="patientForm.dispatchEmergens.arrivedHospitalTime" type="datetime" size="mini"
                  value-format="yyyy-MM-dd HH:mm:ss" />
              </el-form-item>
            </el-col> -->
          </div>

          <!-- <div v-if="patientForm.comingWayCode === '9'">
            <el-col :span="8">
              <el-form-item label="到达医院大门时间" label-width="200px" required>
                <el-date-picker
                  v-model="patientForm.dispatchEmergens.arrivedHospitalTime"
                  type="datetime"
                  size="mini"
                  value-format="yyyy-MM-dd HH:mm:ss"
                />
              </el-form-item>
            </el-col>
          </div> -->
          <!-- <el-col :span="8">
            <el-form-item label="首次接触医疗时间" label-width="200px">
              <el-date-picker v-model="patientForm.dispatchEmergens.firstMedicalContactDateTime" type="datetime"
                size="mini" value-format="yyyy-MM-dd HH:mm:ss" />
            </el-form-item>
          </el-col> -->
          <div v-if="patientForm.comingWayCode === '8'">
            <el-col :span="8">
              <el-form-item label="转出医院入门时间" label-width="200px">
                <el-date-picker
                  v-model="patientForm.dispatchEmergens.outhospitalVisitTime"
                  type="datetime"
                  size="mini"
                  value-format="yyyy-MM-dd HH:mm:ss"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="决定转院时间" label-width="200px">
                <el-date-picker
                  v-model="patientForm.dispatchEmergens.transferTime"
                  type="datetime"
                  size="mini"
                  value-format="yyyy-MM-dd HH:mm:ss"
                />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="转出医院出门时间" label-width="200px">
                <el-date-picker
                  v-model="patientForm.dispatchEmergens.leaveOuthospitalTime"
                  type="datetime"
                  size="mini"
                  value-format="yyyy-MM-dd HH:mm:ss"
                />
              </el-form-item>
            </el-col>
            <!-- <el-col :span="8">
              <el-form-item label="到达医院大门时间" label-width="200px">
                <el-date-picker v-model="patientForm.dispatchEmergens.arrivedHospitalTime" type="datetime" size="mini"
                  value-format="yyyy-MM-dd HH:mm:ss" />
              </el-form-item>
            </el-col> -->
            <el-col :span="8">
              <el-form-item label="院内接诊时间" label-width="200px" required>
                <el-date-picker
                  v-model="patientForm.dispatchEmergens.inhospitalAdmissionTime"
                  type="datetime"
                  size="mini"
                  value-format="yyyy-MM-dd HH:mm:ss"
                />
              </el-form-item>
            </el-col>
          </div>
          <el-col v-if="patientForm.comingWayCode !== '8'" :span="8">
            <el-form-item label="院内首诊医师接诊时间" label-width="200px" :required="patientForm.comingWayCode !== '10'">
              <el-date-picker
                v-model="patientForm.dispatchEmergens.inhospitalAdmissionTime"
                type="datetime"
                size="mini"
                value-format="yyyy-MM-dd HH:mm:ss"
              />
            </el-form-item>
          </el-col>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
<script>
import {
  selectRegionEx,
  getPatientStroke,
  savePatientInfo,
  getPatientsImg,
  getDepartmentList,
  savePatientImg
} from '@/api/green-channel/info'

export default {
  name: 'EmergencyTreatment',
  // 由主页面(父组件) 接收来的, 选择项的数据
  // eslint-disable-next-line vue/require-prop-types
  props: ['checkrowdata'], // 由主页面(父组件) 接收来的, 选择项的数据
  data() {
    return {
      patientForm: {
        dispatchEmergens: {}
      },

      rules: {}
    }
  },
  created() {
    this.getPatientInfo()
  },
  methods: {
    update() {
      let whether = true
      this.patientForm.isKnownAttackTime = this.patientForm.isKnownAttackTime ? this.patientForm.isKnownAttackTime : '0'
      this.patientForm.patientId = this.checkrowdata.patientId
      this.patientForm.dispatchId = this.checkrowdata.dispatchId
      if (!this.patientForm.dispatchEmergens.firstMedicalContactDateTime) {
        this.$message.warning('首次医疗接触时间必须填写!')
        whether = false
      } else if (!this.patientForm.incidentDateTime) {
        this.$message.warning('患者发病时间必须填写!')
        whether = false
      }
      if (!this.patientForm.dispatchEmergens.arrivalTime) {
        this.$message.warning('到达医院大门时间必须填写!')
        whether = false
      }

      if ((this.patientForm.comingWayCode === '6' || this.patientForm.comingWayCode === '7' || this.patientForm.comingWayCode === '9') && !this.patientForm.dispatchEmergens.inhospitalAdmissionTime) {
        this.$message.warning('院内首诊医师接诊时间必须填写!')
        whether = false
      }
      if ((this.patientForm.comingWayCode === '1' || this.patientForm.comingWayCode === '2') && !this.patientForm.dispatchEmergens.inhospitalAdmissionTime) {
        this.$message.warning('院内接诊时间必须填写!')
        whether = false
      }
      if (this.patientForm.comingWayCode === '6' || this.patientForm.comingWayCode === '7') {
        if (this.checkTime(this.patientForm.dispatchEmergens.helpDate, this.patientForm.dispatchEmergens.dispatch120Time)) {
          this.$message.warning('呼救时间必须小于派车时间!')
          return
        }
        if (this.checkTime(this.patientForm.dispatchEmergens.dispatch120Time, this.patientForm.dispatchEmergens.departure120Time)) {
          this.$message.warning('派车时间必须小于出车时间!')
          return
        }
        if (this.checkTime(this.patientForm.dispatchEmergens.departure120Time, this.patientForm.dispatchEmergens.arrivalScene120Time)) {
          this.$message.warning('出车时间必须小于120到达现场时间!')
          return
        }
        if (this.checkTime(this.patientForm.dispatchEmergens.arrivalScene120Time, this.patientForm.dispatchEmergens.leaveScene120Time)) {
          this.$message.warning('120到达现场时间必须小于120离开现场时间!')
          return
        }
        if (this.checkTime(this.patientForm.dispatchEmergens.leaveScene120Time, this.patientForm.dispatchEmergens.arrivedHospitalTime, 1)) {
          this.$message.warning('120离开现场时间必须小于等于患者送达医院时间!')
          return
        }
        if (this.checkTime(this.patientForm.dispatchEmergens.arrivedHospitalTime, this.patientForm.dispatchEmergens.inhospitalAdmissionTime)) {
          this.$message.warning('患者送达医院时间必须小于院内首诊医师接诊时间!')
          return
        }
      }
      if (this.patientForm.comingWayCode === '9' || this.patientForm.comingWayCode === '10' || this.patientForm.comingWayCode === '8') {
        if (this.patientForm.comingWayCode === '9') {
          if (this.checkTime(this.patientForm.incidentDateTime, this.patientForm.dispatchEmergens.arrivedHospitalTime)) {
            this.$message.warning('发病时间必须小于到达医院大门时间!')
            return
          }
          if (this.checkTime(this.patientForm.arrivedHospitalTime, this.patientForm.dispatchEmergens.firstMedicalContactDateTime, 1)) {
            this.$message.warning('到达医院大门时间必须小于等于首次医疗接触时间!')
            return
          }
        }
        if (this.patientForm.comingWayCode === '8') {
          if (this.checkTime(this.patientForm.arrivedHospitalTime, this.patientForm.dispatchEmergens.firstMedicalContactDateTime)) {
            this.$message.warning('到达医院大门时间必须小于首次医疗接触时间!')
            return
          }
        }
      }
      if (this.checkTime(this.patientForm.firstMedicalContactDateTime, this.patientForm.dispatchEmergens.inhospitalAdmissionTime, 1)) {
        this.$message.warning('首次医疗接触时间必须小于等于院内首诊医师接诊时间!')
        return
      }

      if (whether) {
        savePatientInfo(this.patientForm).then((res) => {
          if (res.code === 0) {
            this.$message.success('保存成功')
            this.getPatientInfo()
          } else {
            this.$message.error('保存失败')
          }
        })
      }
    },
    checkTime(time1, time2, type) {
      if (time1 && time2) {
        if (!type) {
          if (new Date(time1).getTime() >= new Date(time2).getTime()) {
            return true
          }
        } else {
          if (new Date(time1).getTime() > new Date(time2).getTime()) {
            return true
          }
        }
      }
    },

    getPatientInfo() {
      getPatientStroke(
        this.checkrowdata.patientId,
        this.checkrowdata.dispatchId
      ).then((res) => {
        console.log(res, 'res')
        this.patientForm = res.data
        this.comingWayCode = res.data.comingWayCode
        this.patientForm.dispatchEmergens = this.patientForm.dispatchEmergens || {}
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.info {
  overflow-x: auto;
  min-height: 80vh;

  .equal-row-height {
    line-height: 36px;
  }

  .form-row {
    .equal-row-height {
      margin-left: 20px;
    }
  }

  .el-form-item {
    margin-bottom: 10px;
  }

  .in-hospital {
    .title {
      width: 128px;
      font-size: 14px;
      color: #606266;
      text-align: right;
      padding-right: 12px;
      line-height: 36px;
    }
  }

  .line {
    height: 1px;
    width: 100%;
    border-top: 1px dashed #dcdfe6;
    margin: 10px 0 9px 0;
  }

  .outerBox {
    position: relative;
  }

  .icon-style {
    position: absolute;
    top: 0px;
    right: 0px;
    z-index: 99;
    font-size: 20px;
    font-weight: 700;
  }

  .icon-style:hover {
    cursor: pointer;
  }
}
</style>
<style lang="scss">
.info {
  .el-form-item__label {
    font-weight: normal;
  }

  .not-accurate-minutes {
    .el-input__inner {
      width: 130px;
      min-width: unset;
    }
  }

  .short-text {
    .el-input__inner {
      width: 85px;
      min-width: unset;
    }
  }

  .el-input__inner {
    min-width: 220px;
  }

  .radio-div {
    min-width: 220px;
  }

  .long-text {
    .el-input__inner {
      min-width: 545px;
    }
  }
}
</style>

<style scoped>
.table-bordered {
  border: 1px solid #ddd;
}

.table {
  width: 100%;
  max-width: 100%;
  margin-bottom: 20px;
}

th,
td {
  padding: 5px;
  border: 1px solid #ddd;
}

th {
  width: 200px;
}

td {
  width: 500px;
}

.pageForm {
  padding: 0 30px;
}
</style>
